package com.liaoban.app.ui.mine;

import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.adapter.AlbumAdapter;
import com.liaoban.app.api.type.Img;
import com.liaoban.app.api.type.request.ImgDelRequest;
import com.liaoban.app.api.type.request.ImgListRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.anchor.ImgBannerActivity;
import com.liaoban.app.ui.base.BaseListActivity;
import com.liaoban.app.util.CommonUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ybc on 2018/7/13 0013.
 * 我的相册
 */
public class AlbumActivity extends BaseListActivity<Img> {
    private List<Img> mImgs = new ArrayList<>(); //保存所有图片数据

    @Override
    protected void setRecyclerView(RecyclerView recyclerView) {
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    public String getNewTitle() {
        return getString(R.string.my_album);
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return new AlbumAdapter(null);
    }

    @Override
    public void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        switch (view.getId()) {
            case R.id.ic_del:
                Img img = (Img) baseQuickAdapter.getData().get(position);
                showRunningDialog(R.string.committing);
                ImgDelRequest imgDelRequest = new ImgDelRequest();
                imgDelRequest.setImgId(img.getImgId() + "");
                startTask(CommonBiz.getInstance().imgDelete(imgDelRequest), baseResponse -> {
                    ToastHelper.show(this, baseResponse.msg);
                    if (1 == baseResponse.code) {
                        baseQuickAdapter.remove(position);
                        mImgs.remove(position);
                    }
                });
                break;
            case R.id.iv_add:
                showSheetDialog(false);
                break;
        }
    }

    @Override
    public void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        List<Img> imgs = baseQuickAdapter.getData();
        List<String> pics = new ArrayList<>();
        for (int i = 0; i < (imgs.size() - 1); i++) {
            Img img = imgs.get(i);
            pics.add(CommonUtils.getFullPic(img.getImgSrc()));
        }
        startActivity(ImgBannerActivity.newIntent(this, pics, position));
    }

    @Override
    public void getData() {
        ImgListRequest imgListRequest = new ImgListRequest();
        imgListRequest.setWuserId(ConfigHelper.getString(this, ConfigKey.USER_ID));
        startTask(CommonBiz.getInstance().imgList(imgListRequest), listBaseResponse -> {
            doSuc(listBaseResponse.data, listBaseResponse.totalPageCount);
        });
    }

    @Override
    //数据请求成功后的结果处理
    public void doSuc(List<Img> rows, int totalPageCount) {
        swipeLayout.setRefreshing(false);
        swipeLayout.setEnabled(true);
        mAdapter.loadMoreComplete();
        if (mPageIndex == 1) {
            if (mImgs.size() > 0) {
                mImgs.clear();
            }
        }
        mImgs.addAll(rows);
        List<Img> tempImgs = new ArrayList<>();
        tempImgs.addAll(mImgs);
        tempImgs.add(new Img()); //最后面添加一个添加图片的图标
        mAdapter.setNewData(tempImgs);
        if (mPageIndex++ >= totalPageCount) {
            mAdapter.loadMoreEnd(false);
        }
    }

    @Override
    protected void uploadPicSuccessful(boolean isTailor, File outFile, Uri filePath) {
        super.uploadPicSuccessful(isTailor, outFile, filePath);
        showRunningDialog(R.string.committing);
        startTask(CommonBiz.getInstance().imgUpload(outFile), baseResponse -> {
            ToastHelper.show(this, baseResponse.msg);
            mImgs.add(0, baseResponse.data);
            List<Img> tempImgs = new ArrayList<>();
            tempImgs.addAll(mImgs);
            tempImgs.add(new Img()); //最后面添加一个添加图片的图标
            mAdapter.setNewData(tempImgs);
        });
    }

    @Override
    protected boolean isPage() {
        return false;
    }
}
